package org.itli.jnsocket.Service.ServiceImpl;

import com.google.gson.Gson;
import jakarta.validation.constraints.Size;
import lombok.extern.slf4j.Slf4j;
import org.itli.jnsocket.Mapper.WareHouseMapper;
import org.itli.jnsocket.Pojo.WareHouse;
import org.itli.jnsocket.Service.WareHouseService;
import org.itli.jnsocket.Util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class WareHouseServiceImpl implements WareHouseService {

    WareHouseMapper mapper;
    Gson gson;

    @Autowired
    public void setMapper(WareHouseMapper mapper) {
        this.mapper = mapper;
    }

    @Autowired
    public void setGson(Gson gson) {
        this.gson = gson;
    }

    @Override
    public String AllWH(PageUtil page) {
        try{
            List<WareHouse> wareHouses = mapper.selectAll(page.getSize(),page.getPage());
            return gson.toJson(wareHouses);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return "error";
        }
    }

    @Override
    public String AddWH(WareHouse wareHouse, String username) {
        try{
            int i = mapper.AddWH(wareHouse, username, username);
            if(i==1){
                return "1";
            }
            else {
                return "0";
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return "error";
        }
    }

    @Override
    public String UpdateWH(WareHouse wareHouse, String username) {
        try{
            int i = mapper.UpdateWH(wareHouse, username);
            if(i==1){
                return "1";
            }
            else {
                return "0";
            }
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return "error";
        }
    }

    @Override
    public String DeleteWH(WareHouse wareHouse, String username) {
        try{
            mapper.Delete(Integer.parseInt(wareHouse.getId()));
            return "1";
        } catch (Exception e) {
            return "error";
        }
    }
}
